const app = getApp();
import { zg99Request, hbappRequest } from "/utils/requests";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    myPhone: '',
    actid: '',
    prizes: '',
    options: [],
    gonghao: '',
    fenxiao: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data1 = JSON.parse(decodeURIComponent(options.data));
    this.setData({
      myPhone: data1.myPhone,
      actid: data1.actid,
      result: data1.result,
      prizes: data1.prizes,
      options: data1.options,
      id: data1.options.id,
      hdid: data1.options.hdid,
      gonghao: data1.options.gonnghao,
      fenxiao: data1.options.fenxiao,
      options: data1.options
    })
  },
  //去抽奖
  gotolottery: function (a) {
    xhs.redirectTo({
      url: "./index?data=" + JSON.stringify(this.data.options)
    });
  },
  // 兑换
  exchange(e) {
    const {
      id,
      index
    } = e.currentTarget.dataset;
    xhs.showModal({
      title: '是否兑换',
      content: '请由现场工作人员点击兑换，自己点击将无法兑换奖品！',
      complete: (res) => {
        if (res.confirm) {
          zg99Request({
            url: `/index/choujiang/updatejlb?actid=${this.data.actid}&callback=&shijian=${Date.parse(new Date()) / 1e3}&`,
            type: 'GET',
            dataType: 'jsonp',
            data: {
              tabnum: 1,
              id,
              phone: this.data.myPhone,
              exchanged: 'true'
            },
          }).then(res => {
            let newStr = app.methods.getAPIReturn(res);
            if (newStr.status == 1) {
              let { result } = this.data;
              result[index].exchanged = 'true';
              this.setData({
                result
              })
              xhs.showToast({
                title: '兑换成功！',
                icon: 'success',
                duration: 2000,
                mask: true
              })
            } else {
              xhs.showToast({
                title: '兑换失败，请联系客服老师',
                icon: 'error',
                duration: 2000,
                mask: true
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})